const EVENT_SCROLL_START = "event_scroll_start";
const EVENT_SCROLL_CHANGE = "event_scroll_change";
const EVENT_SCROLL_FINISH = "event_scroll_finish";
const EVENT_SCROLL_PRESSUP = "event_scroll_pressup";

function init_scroll (ct, mk, contentSizeMc) {
  let that = this;
  let content = ct;

  let mask = new createjs.Shape();

  mask.width = mk.width;
  mask.height = mk.height;
  mask.graphics.beginFill("#ffffff").drawRect(mk.x, mk.y, mask.width, mask.height);

  content.mask = mask;

  // content.regX = 0;
  // content.regY = 0;

  let bg = new createjs.Shape();

  let width = contentSizeMc?contentSizeMc.getTransformedBounds().width:content.getBounds().width;
  let height = contentSizeMc?contentSizeMc.getTransformedBounds().height:content.getBounds().height;

  bg.width = width;
  bg.height = height;
  bg.graphics.beginFill("#ffffff").drawRect(0, 0, bg.width, bg.height);
  bg.name = "bg";

  let bg_pt = content.parent.localToLocal(content.x, content.y, content);
  bg.x = bg_pt.x;
  bg.y = bg_pt.y;
  bg.alpha = 0.01;
  content.addChild(bg);

  let idx = content.getChildIndex(bg);

  for (let i = idx; i>0 ; i--) {
    content.swapChildrenAt(i, i-1)
  }

  let s_x = content.x;
  let s_y = content.y;
  let e_x = s_x + mk.width;
  let e_y = s_y + mk.height;

  let stageY = 0;
  let stageX = 0;
  let timeStamp = 0;
  let speedY = 0;
  let speedX = 0;
  let startX = 0;
  let startY = 0;

  let touched = false;
  let ticking = false;

  content.addEventListener("mousedown", handleStart);

  function handleStart (e) {
    timeStamp = e.timeStamp;
    let pt = {x: e.stageX/pRatio/sRatio*stage.scaleX, y: e.stageY/pRatio/sRatio*stage.scaleY};
        // content.localToLocal(e.stageX, e.stageY, content.parent);
    startX = stageX = pt.x;
    startY = stageY = pt.y;
    touched = true;

    let evt = new Event(EVENT_SCROLL_START);
    // evt.scroll = content.y/e_y;
    content.dispatchEvent(evt);
    content.addEventListener("pressup", handleEnd);

    let bounds_width = contentSizeMc?contentSizeMc.getTransformedBounds().width:content.getBounds().width;
    let bounds_height = contentSizeMc?contentSizeMc.getTransformedBounds().height:content.getBounds().height;

    if(bounds_height>mk.height||bounds_width>mk.width) {
      content.addEventListener("pressmove", handleMove);

      if(!ticking) {
        content.addEventListener("tick", onScorllTick);
        ticking = true;
      }
    }

    e.stopPropagation();
  }

  function handleEnd (e){
    content.removeEventListener("pressup", handleEnd);
    content.removeEventListener("pressmove", handleMove);
    touched = false;
    e.stopPropagation();

    let pt = {x: e.stageX/pRatio/sRatio*stage.scaleX, y: e.stageY/pRatio/sRatio*stage.scaleY};

    if(pt.x-startX==0&&pt.y-stageY==0) {
      for (let i = 0; i < content.children.length; i++) {
        let children = content.children[i];

        if (children.children) {
          for (let j = 0; j < children.children.length; j++) {
            let _c = children.children[j];

            if (_c.getTransformedBounds()) {
              let pt_ = children.localToGlobal(_c.x, _c.y);
              if(pt.x>pt_.x&&pt.x<pt_.x+_c.getTransformedBounds().width*stage.scaleX) {
                if(pt.y>pt_.y&&pt.y<pt_.y+_c.getTransformedBounds().height*stage.scaleY){
                  // console.log(_c.name)
                  var event = new Event("click");
                  event.stageX = e.stageX;
                  event.stageY = e.stageY;
                  _c.dispatchEvent(event);
                }
              }
            }
          }
        }

        if (children.getTransformedBounds()) {
          let pt_ = content.localToGlobal(children.x, children.y);
          if(pt.x>pt_.x&&pt.x<pt_.x+children.getTransformedBounds().width*stage.scaleX) {
            if(pt.y>pt_.y&&pt.y<pt_.y+children.getTransformedBounds().height*stage.scaleY){
              // console.log(children.name)
              var event = new Event("click");
              event.stageX = e.stageX;
              event.stageY = e.stageY;
              children.dispatchEvent(event);
            }
          }
        }
      }
    } else {
      let evt = new Event(EVENT_SCROLL_PRESSUP);
      evt.scroll = content.x/e_x;
      content.dispatchEvent(evt);
    }
  }

  function handleMove (e) {
    let t = e.timeStamp-timeStamp;
    // let pt = content.localToLocal(e.stageX, e.stageY, content.parent);
    let pt = {x: e.stageX/pRatio/sRatio*stage.scaleX, y: e.stageY/pRatio/sRatio*stage.scaleY};

    let offsetX = (pt.x-stageX)/pRatio/sRatio*stage.scaleX;
    let offsetY = (pt.y-stageY)/pRatio/sRatio*stage.scaleY;

    // console.log(content.children)

    for (let i = 0; i < content.children.length; i++) {
        let children = content.children[i];

        if (children.name == "bg") {
            continue;
        }

        // children.mouseEnabled = false;
    }

    speedY = (pt.y-stageY)/t;
    speedX = (pt.x-stageX)/t;

    stageX = pt.x;
    stageY = pt.y;

    timeStamp = e.timeStamp;

    if(content.y+offsetY>s_y) {
      offsetY = s_y-content.y;
      speedY = 0;
    }

    let bounds_width = contentSizeMc?contentSizeMc.getTransformedBounds().width:content.getBounds().width;
    let bounds_height = contentSizeMc?contentSizeMc.getTransformedBounds().height:content.getBounds().height;

    if(content.y+bounds_height+offsetY<e_y) {
      offsetY = e_y-(content.y+bounds_height);
      speedY = 0;
    }

    if(content.x+offsetX>s_x) {
      offsetX = s_x-content.x;
      speedX = 0;
    }

    if(content.x+bounds_width+offsetX<e_x) {
      offsetX = e_x-(content.x+bounds_width);
      speedX = 0;
    }

    // console.log(offsetX)

    content.y+=offsetY;
    content.x+=offsetX;

    if(offsetY!=0) {
      let evt = new Event(EVENT_SCROLL_CHANGE);
      evt.scroll = content.y<0?-1*(e_y+Math.abs(content.y))/contentSizeMc.getTransformedBounds().height:(e_y+Math.abs(content.y))/contentSizeMc.getTransformedBounds().height;

      // console.log("##########", evt.scroll)
      content.dispatchEvent(evt);
      // e.stopPropagation();
    }

    if(offsetX!=0) {
      let evt = new Event(EVENT_SCROLL_CHANGE);
      evt.scroll = (e_y+Math.abs(content.y))/contentSizeMc.getTransformedBounds().height;

      // console.log("##########", evt.scroll)


      content.dispatchEvent(evt);
      // e.stopPropagation();
    }
  }

  function onScorllTick(e) {
    if(Math.abs(speedY)>0) {
      speedY += -speedY/10;
      if(Math.abs(speedY)<=0.01) {
        speedY = 0;
      }
    }

    if(Math.abs(speedX)>0) {
      speedX += -speedX/10;
      if(Math.abs(speedX)<=0.01) {
        speedX = 0;
      }
    }

    // if(speedY==0&&!touched) {
    //   content.removeEventListener("tick", onScorllTick);
    //   ticking = false;
    // }

    let bounds_width = contentSizeMc?contentSizeMc.getTransformedBounds().width:content.getBounds().width;
    let bounds_height = contentSizeMc?contentSizeMc.getTransformedBounds().height:content.getBounds().height;

    if(speedX==0&&speedY==0&&!touched) {
      content.removeEventListener("tick", onScorllTick);
      ticking = false;

      for (let i = 0; i < content.children.length; i++) {
        let children = content.children[i];
        children.mouseEnabled = true;
      }

      let evt = new Event(EVENT_SCROLL_FINISH);


      // console.log(contentSizeMc.getTransformedBounds().height, e_y, content.y)

      evt.scroll = (e_y+Math.abs(content.y))/contentSizeMc.getTransformedBounds().height;
      content.dispatchEvent(evt);

      // console.log("########**", evt.scroll)
    }

    if(!touched) {
      let offsetY = speedY*e.delta/stage.scaleY/stage.scaleY;
      let offsetX = speedX*e.delta/stage.scaleX/stage.scaleX;

      if(content.y+offsetY>s_y) {
        offsetY = s_y-content.y;
        speedY = 0;
      }

      if(content.y+bounds_height+offsetY<e_y) {
        offsetY = e_y-content.y-bounds_height;
        speedY = 0;
      }

      if(content.x+offsetX>s_x) {
        offsetX = s_x-content.x;
        speedX = 0;
      }

      if(content.x+bounds_width+offsetX<e_x) {
        offsetX = e_x-content.x-bounds_width;
        speedX = 0;
      }


      content.y+=isNaN(offsetY)?0:offsetY;
      content.x+=offsetX;

      if(offsetY!=0) {
        let evt = new Event(EVENT_SCROLL_CHANGE);
        evt.scroll = (e_y+Math.abs(content.y))/contentSizeMc.getTransformedBounds().height;
        content.dispatchEvent(evt);
      }

      if(offsetX!=0) {
        let evt = new Event(EVENT_SCROLL_CHANGE);
        evt.scroll = content.x/e_x;
        content.dispatchEvent(evt);
      }
    }
  }

  function reset() {
    content.removeEventListener("pressup", handleEnd);
    content.removeEventListener("pressmove", handleMove);
    content.removeEventListener("tick", onScorllTick);

    touched = false;
    content.y = 0;
    content.x = 0;
    speedY==0
    speedX==0;

    let bounds_width = contentSizeMc?contentSizeMc.getTransformedBounds().width:content.getBounds().width;
    let bounds_height = contentSizeMc?contentSizeMc.getTransformedBounds().height:content.getBounds().height;

    e_x = mk.width - (s_x + bounds_width);
    e_y = mk.height - (s_y + bounds_height);
  }

  function distory() {
    content.removeAllEventListeners(EVENT_SCROLL_CHANGE);
    content.removeEventListener("mousedown", handleStart);
    content.removeEventListener("tick", onScorllTick);
    if(that.removeAllEventListeners) {
      that.removeAllEventListeners("click");
    }
    ticking = false;
  }

  this.reset = reset;
  this.distory = distory;
  return this;
}

createjs.EventDispatcher.initialize(init_scroll.prototype);
